package com.insyte.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.Profile;
import com.insyte.R;
import com.insyte.activities.ProfileActivity;
import com.insyte.config.Constants;
import com.insyte.config.InsyteApp;
import com.insyte.model.User;
import com.insyte.utils.NonSwipeableViewPager;

import antistatic.spinnerwheel.AbstractWheel;
import antistatic.spinnerwheel.OnWheelChangedListener;
import antistatic.spinnerwheel.WheelVerticalView;
import antistatic.spinnerwheel.adapters.ArrayWheelAdapter;

public class ProfileInfoFragment extends Fragment implements View.OnClickListener {
    private View view;
    private Button btn_male, btn_female, btn_next;
    private WheelVerticalView set_age, set_height;
    private String[] arr_age, arr_height;
    private RelativeLayout btn_set_age, btn_set_height;
    private String current_age, current_height = "0";
    private ImageView btn_close, btn_save, btn_close_height ,btn_save_height;
    private ImageView imgBack;
    private TextView tv_age, tv_height;
    private EditText edt_story;
    private LinearLayout ly_primary, ly_disable;
    private LinearLayout ly_set_age, ly_set_height;
    private String gender_current = "male";
    private int height_window = 0;
    private User user;
    private NonSwipeableViewPager viewPager;
    float fCloseHeight = 22;
    float fDoneHeightH = 19;

    public ProfileInfoFragment setViewPager(NonSwipeableViewPager viewPager) {
        this.viewPager = viewPager;
        return this;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_profile_info, container, false);
        getViewAgeHeight();
        setEvent();
        setWheel();
        edt_story.setFocusableInTouchMode(false);
        height_window = getActivity().getWindowManager().getDefaultDisplay().getHeight();
        ly_set_age.setY(height_window);
        ly_set_height.setY(height_window);
        user = InsyteApp.getInstance().getUser();
        getUserinfo();
        return view;
    }

    private void getViewAgeHeight() {
        btn_set_age = (RelativeLayout) view.findViewById(R.id.btn_set_age);
        btn_set_height = (RelativeLayout) view.findViewById(R.id.btn_set_height);
        set_age = (WheelVerticalView) view.findViewById(R.id.set_age);
        set_height = (WheelVerticalView) view.findViewById(R.id.set_height);
        btn_close = (ImageView) view.findViewById(R.id.btn_close);
//        btn_close.getLayoutParams().width = (int)(fCloseHeight* Constants.fScale);
//        btn_close.getLayoutParams().height = (int)(fCloseHeight* Constants.fScale);
        imgBack = (ImageView) view.findViewById(R.id.imgBack);
        btn_save = (ImageView) view.findViewById(R.id.btn_save);
//        btn_save.getLayoutParams().width = (int)(fCloseHeight* Constants.fScale);
//        btn_save.getLayoutParams().height = (int)(fDoneHeightH* Constants.fScale);
        btn_close_height = (ImageView) view.findViewById(R.id.btn_close_height);
//        btn_close_height.getLayoutParams().width = (int)(fCloseHeight* Constants.fScale);
//        btn_close_height.getLayoutParams().height = (int)(fCloseHeight* Constants.fScale);
        btn_save_height = (ImageView) view.findViewById(R.id.btn_save_height);
//        btn_save_height.getLayoutParams().width = (int)(fCloseHeight* Constants.fScale);
//        btn_save_height.getLayoutParams().height = (int)(fDoneHeightH* Constants.fScale);
        tv_age = (TextView) view.findViewById(R.id.tv_age);
        tv_height = (TextView) view.findViewById(R.id.tv_height);
        ly_primary = (LinearLayout) view.findViewById(R.id.ly_primary);
        ly_disable = (LinearLayout) view.findViewById(R.id.ly_disable);
        ly_set_age = (LinearLayout) view.findViewById(R.id.ly_set_age);
        ly_set_height = (LinearLayout) view.findViewById(R.id.ly_set_height);
        btn_male = (Button) view.findViewById(R.id.btn_male);
        btn_female = (Button) view.findViewById(R.id.btn_female);
        btn_next = (Button) view.findViewById(R.id.btn_next);
        edt_story = (EditText) view.findViewById(R.id.edt_story);
    }

    private void setEvent() {
        btn_set_age.setOnClickListener(this);
        btn_set_height.setOnClickListener(this);
        btn_close.setOnClickListener(this);
        btn_save.setOnClickListener(this);
        imgBack.setOnClickListener(this);
        btn_close_height.setOnClickListener(this);
        btn_save_height.setOnClickListener(this);
        btn_male.setOnClickListener(this);
        btn_female.setOnClickListener(this);
        edt_story.setOnClickListener(this);
        btn_next.setOnClickListener(this);

        set_age.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(AbstractWheel wheel, int oldValue, int newValue) {
                current_age = arr_age[newValue];
            }
        });

        set_height.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(AbstractWheel wheel, int oldValue, int newValue) {
                current_height = arr_height[newValue];
            }
        });
    }

    private void setWheel() {
        arr_age = setData(120, 10, 1);
        ArrayWheelAdapter<String> ageAdapter = new ArrayWheelAdapter<String>(getActivity(), arr_age);
        ageAdapter.setItemResource(R.layout.wheel_item);
        ageAdapter.setItemTextResource(R.id.text_wheel);
        set_age.setAllItemsVisible(true);
        set_age.setViewAdapter(ageAdapter);
        arr_height = setData(400, 100, 1);
        ArrayWheelAdapter<String> heightAdapter = new ArrayWheelAdapter<String>(getActivity(), arr_height);
        ageAdapter.setItemResource(R.layout.wheel_item);
        ageAdapter.setItemTextResource(R.id.text_wheel);
        set_height.setAllItemsVisible(true);
        set_height.setViewAdapter(heightAdapter);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_set_age:
                ly_set_age.animate().translationY(height_window - ly_set_age.getHeight());
                ly_disable.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_set_height:
                ly_set_height.animate().translationY(height_window - ly_set_age.getHeight());
                ly_disable.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_save:
                tv_age.setText(current_age);
                ly_set_age.animate().translationY(height_window);
                ly_disable.setVisibility(View.GONE);
                break;
            case R.id.btn_save_height:
                tv_height.setText(current_height);
                ly_set_height.animate().translationY(height_window);
                ly_disable.setVisibility(View.GONE);
                break;
            case R.id.btn_close:
            case R.id.btn_close_height:
                ly_set_age.animate().translationY(height_window);
                ly_set_height.animate().translationY(height_window);
                ly_disable.setVisibility(View.GONE);
                break;
            case R.id.btn_male:
                btn_male.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_orange_rectange_fill_rounded));
                btn_female.setBackgroundDrawable(getResources().getDrawable(R.drawable.background_edittext));
                btn_male.setTextColor(getResources().getColor(android.R.color.white));
                btn_female.setTextColor(getResources().getColor(R.color.primary_orange));
                gender_current = "male";
                break;
            case R.id.btn_female:
                btn_female.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_orange_rectange_fill_rounded));
                btn_male.setBackgroundDrawable(getResources().getDrawable(R.drawable.background_edittext));
                btn_female.setTextColor(getResources().getColor(android.R.color.white));
                btn_male.setTextColor(getResources().getColor(R.color.primary_orange));
                gender_current = "female";
                break;
            case R.id.edt_story:
                edt_story.setFocusableInTouchMode(true);
                edt_story.requestFocus();
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.showSoftInput(edt_story, InputMethodManager.SHOW_IMPLICIT);
                }
                break;
            case R.id.btn_next:
                user.setGender(gender_current);
                user.setAge(tv_age.getText().toString());
                user.setHeight(tv_height.getText().toString());
                user.setStory(edt_story.getText().toString());
                InsyteApp.getInstance().saveUser(user);
                viewPager.setCurrentItem(1);
                break;
            case R.id.imgBack:
                //getActivity().onBackPressed();
                getActivity().finish();
                break;
        }
    }

    private String[] setData(int total, float start, float jump) {
        String[] arr = new String[total];
        int j = 0;
        for (float i = start; j < total; i += jump) {
            arr[j] = i + "";
            j++;
        }
        return arr;
    }

    private void getUserinfo() {
        //Log.e("tuoingon",user.getAge() + "");
        if(user.getAge() !=null) {
            if(user.getAge().length() > 0) {
                tv_age.setText(user.getAge());
            }
        }
        if(user.getHeight() != null) {
            if(user.getHeight().length() > 0) {
                tv_height.setText(user.getHeight());
            }
        }
        if(user.getStory() !=null){
            if(user.getStory().length() > 0) {
                edt_story.setText(user.getStory());
            }
        }
        if(user.getGender() != null) {
            if(user.getGender().equalsIgnoreCase("male")) {
                btn_male.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_orange_rectange_fill_rounded));
                btn_female.setBackgroundDrawable(getResources().getDrawable(R.drawable.background_edittext));
                btn_male.setTextColor(getResources().getColor(android.R.color.white));
                btn_female.setTextColor(getResources().getColor(R.color.primary_orange));
                gender_current = "male";
            }
            else {
                btn_female.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_orange_rectange_fill_rounded));
                btn_male.setBackgroundDrawable(getResources().getDrawable(R.drawable.background_edittext));
                btn_female.setTextColor(getResources().getColor(android.R.color.white));
                btn_male.setTextColor(getResources().getColor(R.color.primary_orange));
                gender_current = "female";
            }
        }
    }
}
